﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageUploader.domain;
using System.DirectoryServices;

namespace ImageUploader.GUI
{
    public partial class AddNewUser : Form
    {
        private DomainController domainController;
        private User myUser;

        public AddNewUser(DomainController dc)
        {
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            InitializeComponent();
            
            txtDistinguishedName.Text = "CN=Users";
            this.domainController = dc;
            myUser = new User();
            
        }

        private void buttonEditUser_Click(object sender, EventArgs e)
        {
            String map = txtDistinguishedName.Text;
            try
            {

                using (DirectoryEntry de = domainController.LDAPConnection.CreateDirectoryEntryWithDomainName(txtDistinguishedName.Text))
                {
                    myUser.Save(de);
                    DirectorySearcher search = new DirectorySearcher(de, "(&(objectClass=User)(cn=" + myUser.CN + "))");
                    myUser.SearchResult = search.FindOne();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

            this.Close();
            EditForm ef = new EditForm(myUser);
            ef.Show();
        }
    }
}
